package com.douqu.game.main.server;

import com.bean.core.util.FileUtils;
import com.bean.core.util.TimeUtils;
import com.bean.core.util.Utils;
import com.douqu.game.core.config.challenge.WorldBossConfig;
import com.douqu.game.core.database.model.DiamondsChangeModel;
import com.douqu.game.core.e.E_BattleDetailType;
import com.douqu.game.core.e.E_CityInOutType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.world.WorldRankBean;
import com.douqu.game.core.entity.world.map.*;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.server.entity.*;
import com.douqu.game.main.service.DiamondsChangeService;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 需要保存到文件的管理器
 * @Author: Bean
 * @Description:
 * @Date: 2018-07-24 16:01
 */
public class RecordManager {

    /**
     * 玩家物品得到和消耗记录
     */
    private List<PlayerGoodsChange> goodsChangeList = new CopyOnWriteArrayList<>();

    /**
     * 玩家的聊天记录
     */
    private List<SGCommonProto.ChatItem> chatList = new CopyOnWriteArrayList<>();

    /**
     * 占领大地图纪录
     */
    private List<RecordWorldMapOccupy> worldMapOccupyList = new CopyOnWriteArrayList<>();

    /**
     * 大地图战斗记录
     */
    private List<RecordWorldMapBattle> worldMapBattleList = new CopyOnWriteArrayList<>();

    /**
     * BOSS战记录
     */
    private List<RecordBossBattle> bossBattleList = new CopyOnWriteArrayList<>();

    /**
     * 排行纪录
     */
    private Map<Integer,List<WorldRankBean>> lastRankMap = new ConcurrentHashMap<>();

    /**
     * 远征军记录
     */
    private List<RecordExpedition> expeditionRecordList = new CopyOnWriteArrayList<>();

    /**
     * 高连杀玩家
     */
    private List<String> continueKillTopPlayerList = new CopyOnWriteArrayList<>();

    private static RecordManager instance = null;

    private RecordManager(){}

    public static RecordManager getInstance()
    {
        if(instance == null)
            instance = new RecordManager();

        return instance;
    }


    public void save()
    {
        savePlayerGoodsChange();
        saveChatRecord();
        saveWorldMapOccupy();
        saveWorldMapBattle();
        saveBossBattle();
        saveInOutCity();
        saveLastRankMap(SGCommonProto.E_RANK_TYPE.RANK_TYPE_BOSS);
        saveLastRankMap(SGCommonProto.E_RANK_TYPE.RANK_TYPE_WORLDMAP);
        saveLastRankMap(SGCommonProto.E_RANK_TYPE.RANK_TYPE_MAPLASTWEEK);
        saveExpedition();
    }

    public void addExpedition(String cityName, String name, String remark)
    {
        RecordExpedition recordExpedition = new RecordExpedition();
        recordExpedition.setCreateTime(DataFactory.currentTime);
        recordExpedition.setCityName(cityName);
        recordExpedition.setRemark(remark);
        recordExpedition.setName(name);

        expeditionRecordList.add(recordExpedition);
    }

    public void saveExpedition()
    {
        if(expeditionRecordList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(RecordExpedition recordExpedition : expeditionRecordList)
        {
            buffer.append(TimeUtils.getFormatTime(recordExpedition.getCreateTime()));
            buffer.append("\t");
            buffer.append(recordExpedition.getCityName());
            buffer.append("\t");
            buffer.append(recordExpedition.getName());
            buffer.append("\t");
            buffer.append(recordExpedition.getRemark());
            buffer.append("\n");
        }

        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/worldMap");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t城名\t远征军\t内容\n";
        File file = new File(directory.getPath()+ "/expedition_" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("远征军信息保存 -> " + file.getAbsolutePath());

        expeditionRecordList.clear();
    }

    public void addLastRankMap(SGCommonProto.E_RANK_TYPE rankType,List<WorldRankBean> list){
        /**
         * 这里画蛇添足的做这一步
         * 1.文件写入进行大量的io操作为了不将线程堵死 特通过新的线程进行
         * 2.在此方法的调用处会对List进行clear 异步可能会造成数据不一致所以复制一份进行处理
         */
        List<WorldRankBean> rankBeanList = new ArrayList<>();
        for (WorldRankBean bean:list){
            rankBeanList.add(bean);
        }
        GameServer.getInstance().startThread(() -> {
            saveLastRankMap(rankType);
            lastRankMap.put(rankType.getNumber(), rankBeanList);
        });
    }


    public void saveLastRankMap(SGCommonProto.E_RANK_TYPE rankType){
        List<WorldRankBean> rankBeanList = lastRankMap.get(rankType.getNumber());
        if(rankBeanList == null)
            return;
        if (rankBeanList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();
        String title = "";
        switch (rankType){
            case RANK_TYPE_BOSS:
                title = new Date() + "\tBOSS战排行\n玩家排行\t玩家流水号\t玩家伤害值\t创建时间\t最后更新时间\n";
                break;
            case RANK_TYPE_WORLDMAP:
                title = new Date() + "\t战场争锋排行\n玩家排行\t玩家流水号\t战场值\t创建时间\t最后更新时间\n";
                break;
            case RANK_TYPE_MAPLASTWEEK:
                title = new Date() + "\t争霸赛\n玩家排行\t玩家流水号\t争霸赛积分\t创建时间\t最后更新时间\n";
                break;
        }
        buffer.append("\n");
        SimpleDateFormat sdf = new SimpleDateFormat();
        WorldRankBean rankBean;
        for (int i = 0;i < rankBeanList.size();i ++){
            rankBean = rankBeanList.get(i);
            buffer.append(i);
            buffer.append("\t");
            buffer.append(rankBean.getObjectIndex());
            buffer.append("\t");
            buffer.append(rankBean.getValue());
            buffer.append("\t");
            buffer.append(sdf.format(rankBean.getCreateTime() * 1000l));
            buffer.append("\t");
            buffer.append(sdf.format(rankBean.getLastUpdateTime() * 1000l));
            buffer.append("\n");
        }
        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/rank");
        if(!directory.exists())
            directory.mkdirs();

        File file = new File(directory.getPath()+ "/rank_" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), title + buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        switch (rankType){
            case RANK_TYPE_BOSS:
                LogUtils.info("BOSS战排行 -> " + file.getAbsolutePath());
                break;
            case RANK_TYPE_WORLDMAP:
                LogUtils.info("战场争锋排行 -> " + file.getAbsolutePath());
                break;
            case RANK_TYPE_MAPLASTWEEK:
                LogUtils.info("争霸赛排行 -> " + file.getAbsolutePath());
                break;
        }

        WorldInfoFactory.getInstance().clearInOutCityRecord();
    }

    private void saveInOutCity()
    {
        List<WorldInfoFactory.InOutCityRecord> list = WorldInfoFactory.getInstance().getInOutCityRecordList();

        if(list.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(WorldInfoFactory.InOutCityRecord record : list)
        {
            if(record.getType() == E_CityInOutType.IN_AUTO_MOVE || record.getType() == E_CityInOutType.IN_DEFAULT_ARRIVE
                    || record.getType() == E_CityInOutType.OUT_AUTO_MOVE || record.getType() == E_CityInOutType.OUT_DEFAULT_ARRIVE)
                continue;

            buffer.append(record.getCreateTime());
            buffer.append("\t");
            buffer.append(record.getPlayerName());
            buffer.append("\t");
            buffer.append(record.getCityName());
            buffer.append("\t");
            buffer.append(record.getRemark());
            buffer.append("\n");
        }

        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/worldMap");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t玩家\t城名\t内容\n";
        File file = new File(directory.getPath()+ "/city_" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("进出城记录保存 -> " + file.getAbsolutePath());

        WorldInfoFactory.getInstance().clearInOutCityRecord();
    }

    private void saveBossBattle()
    {
        if(bossBattleList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(RecordBossBattle bossBattle : bossBattleList)
        {
            buffer.append(bossBattle.getCreateTime());
            buffer.append("\t");
            buffer.append(bossBattle.getPlayerName());
            buffer.append("\t");
            buffer.append(bossBattle.getBossName());
            buffer.append("\t");
            buffer.append(bossBattle.getDamage());
            buffer.append("\t");
            buffer.append(bossBattle.getBossHP());
            buffer.append("\t");
            buffer.append(bossBattle.getExploit());
            buffer.append("\t");
            buffer.append(bossBattle.getExploitType());
            buffer.append("\n");
        }

        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/boss");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t玩家\tBOSS\t伤害值\tBOSS剩余血\t奖励功勋\t备注\n";
        File file = new File(directory.getPath()+ "/" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("BOSS战信息保存 -> " + file.getAbsolutePath());

        bossBattleList.clear();
    }

    private void saveWorldMapBattle()
    {
        if(worldMapBattleList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(RecordWorldMapBattle worldMapBattle : worldMapBattleList)
        {
            buffer.append(worldMapBattle.getCreateTime());
            buffer.append("\t");
            buffer.append(worldMapBattle.getCityName());
            buffer.append("\t");
            buffer.append(worldMapBattle.getType());
            buffer.append("\t");
            buffer.append(worldMapBattle.getBattleDetailType());
            buffer.append("\t");
            buffer.append(worldMapBattle.getBattleId());
            buffer.append("\t");
            buffer.append(worldMapBattle.getRemark());
            buffer.append("\n");
        }

        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/worldMap");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t城名\t类型\t战斗类型\t战斗ID\t内容\n";
        File file = new File(directory.getPath()+ "/battle_" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("大地图战斗信息保存 -> " + file.getAbsolutePath());

        worldMapBattleList.clear();
    }

    private void saveWorldMapOccupy()
    {
        if(worldMapOccupyList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(RecordWorldMapOccupy worldMapOccupy : worldMapOccupyList)
        {
            buffer.append(worldMapOccupy.getCreateTime());
            buffer.append("\t");
            buffer.append(worldMapOccupy.getOccupyPlayerName());
            buffer.append("\t");
            buffer.append(worldMapOccupy.getType());
            buffer.append("\t");
            buffer.append(worldMapOccupy.getRemark());
            buffer.append("\n");
        }

        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/worldMap");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t玩家昵称\t战场类型\t内容\n";
        File file = new File(directory.getPath()+ "/occupy_" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("大地图占领信息保存 -> " + file.getAbsolutePath());

        worldMapOccupyList.clear();
    }

    private void savePlayerGoodsChange()
    {
        if(goodsChangeList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        //保存到数据库
        DiamondsChangeService diamondsChangeService = SpringContext.getBean(DiamondsChangeService.class);
        for(PlayerGoodsChange goodsChange : goodsChangeList)
        {
            if(goodsChange.getGoodsType() == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE
                    && goodsChange.getId() == 1 && goodsChange.getCount() < 0 && goodsChange.getCode() != SGMainProto.E_MSG_ID.MsgID_WorldAuctionBuy_VALUE){
                //统计玩家消耗钻石  拍卖行竞拍不进行数据库存储 但是进行txt存储，在竞拍成功的那一刻已经进行过数据库存储
                diamondsChangeService.insert(new DiamondsChangeModel(
                        TimeUtils.getDateByString(goodsChange.getCreateTime()),
                        goodsChange.getPlayerIndex(),
                        goodsChange.getCount()
                ));
            }
            buffer.append(goodsChange.getCreateTime());
            buffer.append("\t");
            buffer.append(goodsChange.getPlayerIndex());
            buffer.append("\t");
            buffer.append(goodsChange.getPlayerName());
            buffer.append("\t");
            buffer.append(goodsChange.getCode());
            buffer.append("\t");
            buffer.append(goodsChange.getGoodsType());
            buffer.append("\t");
            buffer.append(goodsChange.getId());
            buffer.append("\t");
            buffer.append(goodsChange.getCount());
            buffer.append("\t");
            buffer.append(goodsChange.getRemark());
            buffer.append("\n");
        }


        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/goods/" + date);
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t玩家ID\t玩家昵称\t来源\t类型\tID\t数量\t详情\n";
        String hour = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD + "-" + TimeUtils.HH);
        File file = new File(directory.getPath()+ "/" + hour + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("保存玩家物品数据成功 -> " + file.getAbsolutePath());

        goodsChangeList.clear();
    }


    private void saveChatRecord()
    {
        if(chatList.isEmpty())
            return;

        StringBuilder buffer = new StringBuilder();

        for(SGCommonProto.ChatItem item : chatList)
        {
            buffer.append(TimeUtils.getFormatTime(item.getSendTime()));
            buffer.append("\t");
            buffer.append(item.getPlayerIndex());
            buffer.append("\t");
            buffer.append(item.getNickName());
            buffer.append("\t");
            buffer.append(item.getChatType());
            buffer.append("\t");
            buffer.append(item.getContent());
            buffer.append("\n");
        }


        String date = TimeUtils.getCurrentTime(TimeUtils.YYYYMMDD);
        File directory = new File(ConfigFactory.LOG_PATH + "/chat");
        if(!directory.exists())
            directory.mkdirs();

        String title = "时间\t玩家ID\t玩家昵称\t类型\t内容\n";
        File file = new File(directory.getPath()+ "/" + date + ".txt");
        if(file.exists())
        {
            FileUtils.writeFileAppend(file.getAbsolutePath(), buffer.toString());
        }
        else
        {
            FileUtils.writeFile(file.getAbsolutePath(), title + buffer.toString());
        }

        LogUtils.info("聊天信息保存 -> " + file.getAbsolutePath());

        chatList.clear();
    }

    public void addBossBattle(Player player, String bossId, String bossHPInfo, long damage, int exploit, String exploitType)
    {
        RecordBossBattle recordBossBattle = new RecordBossBattle();
        recordBossBattle.setCreateTime(DataFactory.currentTime);
        recordBossBattle.setPlayerName(player.getNameLv());
        recordBossBattle.setBossHP(bossHPInfo);
        recordBossBattle.setDamage(damage);
        recordBossBattle.setExploit(exploit);
        recordBossBattle.setExploitType(exploitType);

        WorldBossConfig worldBossConfig = DataFactory.getInstance().getGameObject(DataFactory.WORLD_BOSS_KEY, Integer.parseInt(bossId));
        recordBossBattle.setBossName((worldBossConfig==null?bossId:worldBossConfig.name) + "(" + bossId + ")");

        bossBattleList.add(recordBossBattle);
    }


    /**
     * 添加大地图战斗开始记录
     * @param mapMatchTeam
     */
    public void addWorldMapBattleStartRecord(String battleId, MapMatchTeam mapMatchTeam)
    {
        if(Utils.isNullOrEmpty(battleId))
            return;

        addWorldMapBattleStartRecord(battleId, mapMatchTeam.getPlayer(), mapMatchTeam.getTarget(), mapMatchTeam.getCityId(), mapMatchTeam.getBattleDetailType());
    }

    public void addWorldMapBattleStartRecord(String battleId, SpriteBean source, SpriteBean target, int cityId, E_BattleDetailType battleDetailType)
    {
        try{
            RecordWorldMapBattle record = new RecordWorldMapBattle();
            record.setBattleId(battleId);
            record.setBattleDetailType(battleDetailType.name());
            record.setType(Utils.isNullOrEmpty(source.getMasterPlayerIndex()) ? "世界地图战斗开始" : "单人战场战斗开始");
            record.setCreateTime(DataFactory.currentTime);
            record.setCityId(cityId);

            StringBuilder remark = new StringBuilder();
            remark.append("[");
            remark.append(source.getObjectIndex());
            remark.append("-");
            remark.append(source.getNameLv());
            remark.append("] -VS- [");
            remark.append(target.getObjectIndex());
            remark.append("-");
            remark.append(target.getNameLv());
            remark.append("],城详情:");

            WorldMapData worldMapData = WorldInfoFactory.getInstance().getWorldMapData(source.getMasterPlayerIndex());
            if(worldMapData != null)
            {
                CityBean cityBean = worldMapData.getCity(cityId);
                if(cityBean != null)
                {
                    StringBuilder attacker = new StringBuilder("{");
                    StringBuilder defender = new StringBuilder("{");
                    for(SpritePlayerBean spritePlayerBean : cityBean.getSpritePlayerList())
                    {
                        if(spritePlayerBean.isAttacker())
                        {
                            attacker.append("[");
                            attacker.append(spritePlayerBean.getObjectIndex());
                            attacker.append("-");
                            attacker.append(spritePlayerBean.getNameLv());
                            attacker.append("-状态->");
                            attacker.append(spritePlayerBean.getStatus().name());
                            attacker.append("]");
                        }
                        else
                        {
                            defender.append("[");
                            defender.append(spritePlayerBean.getObjectIndex());
                            defender.append("-");
                            defender.append(spritePlayerBean.getNameLv());
                            defender.append("-状态->");
                            defender.append(spritePlayerBean.getStatus().name());
                            defender.append("]");
                        }
                    }

                    for(SpriteGarrisonBean spriteGarrisonBean : cityBean.getSpriteGarrisonList())
                    {
                        defender.append("[");
                        defender.append(spriteGarrisonBean.getObjectIndex());
                        defender.append("-");
                        defender.append(spriteGarrisonBean.getNameLv());
                        defender.append("-状态->");
                        defender.append(spriteGarrisonBean.getStatus().name());
                        defender.append("]");
                    }

                    for(SpriteExpeditionBean spriteExpeditionBean : cityBean.getSpriteExpeditionList())
                    {
                        if(spriteExpeditionBean.isAttacker())
                        {
                            attacker.append("[");
                            attacker.append(spriteExpeditionBean.getObjectIndex());
                            attacker.append("-");
                            attacker.append(spriteExpeditionBean.getNameLv());
                            attacker.append("-状态->");
                            attacker.append(spriteExpeditionBean.getStatus().name());
                            attacker.append("]");
                        }
                        else
                        {
                            defender.append("[");
                            defender.append(spriteExpeditionBean.getObjectIndex());
                            defender.append("-");
                            defender.append(spriteExpeditionBean.getNameLv());
                            defender.append("-状态->");
                            defender.append(spriteExpeditionBean.getStatus().name());
                            defender.append("]");
                        }
                    }

                    attacker.append("}");
                    defender.append("}");

                    remark.append("攻方->");
                    remark.append(attacker.toString());
                    remark.append(",守方->");
                    remark.append(defender.toString());
                }
            }

            record.setRemark(remark.toString());

            worldMapBattleList.add(record);

        }catch (Exception e){
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    /**
     * 添加大地图战斗结束记录
     */
    public void addWorldMapBattleEndRecord(String battleId, SpriteBean winner, SpriteBean loser, CityBean cityBean, int battleDetailType)
    {
        try{
            RecordWorldMapBattle record = new RecordWorldMapBattle();
            record.setBattleId(battleId);
            record.setBattleDetailType(E_BattleDetailType.forNumber(battleDetailType).name());
            record.setType(Utils.isNullOrEmpty(winner.getMasterPlayerIndex()) ? "世界地图战斗结束" : "单人战场战斗结束");
            record.setCreateTime(DataFactory.currentTime);
            record.setCityId(cityBean.id);

            StringBuilder remark = new StringBuilder();
            remark.append(Utils.isNullOrEmpty(battleId) ? "直接比较战力" : "正常战斗,");
            remark.append("胜利方:[");
            remark.append(winner.getObjectIndex());
            remark.append("-");
            remark.append(winner.getNameLv());
            remark.append("-状态->");
            remark.append(winner.getStatus().name());
            remark.append("],失败方:[");
            remark.append(loser.getObjectIndex());
            remark.append("-");
            remark.append(loser.getNameLv());
            remark.append("-状态->");
            remark.append(loser.getStatus().name());
            remark.append("],城状态:");
            remark.append(cityBean.getStatus().name());

            remark.append(",城详情:");

            StringBuilder attacker = new StringBuilder("{");
            StringBuilder defender = new StringBuilder("{");
            for(SpritePlayerBean spritePlayerBean : cityBean.getSpritePlayerList())
            {
                if(spritePlayerBean.isAttacker())
                {
                    attacker.append("[");
                    attacker.append(spritePlayerBean.getObjectIndex());
                    attacker.append("-");
                    attacker.append(spritePlayerBean.getNameLv());
                    attacker.append("-状态->");
                    attacker.append(spritePlayerBean.getStatus().name());
                    attacker.append("]");
                }
                else
                {
                    defender.append("[");
                    defender.append(spritePlayerBean.getObjectIndex());
                    defender.append("-");
                    defender.append(spritePlayerBean.getNameLv());
                    defender.append("-状态->");
                    defender.append(spritePlayerBean.getStatus().name());
                    defender.append("]");
                }
            }

            for(SpriteGarrisonBean spriteGarrisonBean : cityBean.getSpriteGarrisonList())
            {
                defender.append("[");
                defender.append(spriteGarrisonBean.getObjectIndex());
                defender.append("-");
                defender.append(spriteGarrisonBean.getNameLv());
                defender.append("-状态->");
                defender.append(spriteGarrisonBean.getStatus().name());
                defender.append("]");
            }

            for(SpriteExpeditionBean spriteExpeditionBean : cityBean.getSpriteExpeditionList())
            {
                if(spriteExpeditionBean.isAttacker())
                {
                    attacker.append("[");
                    attacker.append(spriteExpeditionBean.getObjectIndex());
                    attacker.append("-");
                    attacker.append(spriteExpeditionBean.getNameLv());
                    attacker.append("-状态->");
                    attacker.append(spriteExpeditionBean.getStatus().name());
                    attacker.append("]");
                }
                else
                {
                    defender.append("[");
                    defender.append(spriteExpeditionBean.getObjectIndex());
                    defender.append("-");
                    defender.append(spriteExpeditionBean.getNameLv());
                    defender.append("-状态->");
                    defender.append(spriteExpeditionBean.getStatus().name());
                    defender.append("]");
                }
            }

            attacker.append("}");
            defender.append("}");

            remark.append("攻方->");
            remark.append(attacker.toString());
            remark.append(",守方->");
            remark.append(defender.toString());

            record.setRemark(remark.toString());

            worldMapBattleList.add(record);
        }catch (Exception e){
            LogUtils.errorException(e);
        }
    }

    public void addWorldMapOccupyRecord(int cityId, OccupyCityParam occupyCityParam)
    {
        RecordWorldMapOccupy worldMapOccupy = new RecordWorldMapOccupy();
        worldMapOccupy.setCreateTime(DataFactory.currentTime);
        worldMapOccupy.setCityId(cityId);
        String temp = "";
        if(!Utils.isNullOrEmpty(occupyCityParam.getWinner().getMasterPlayerIndex()))
        {
            if(occupyCityParam.getWinner().getObjectIndex().equals(occupyCityParam.getWinner().getMasterPlayerIndex()))
                temp = occupyCityParam.getWinner().getNameLv();
            else if(occupyCityParam.getLoser() != null)
                temp = occupyCityParam.getLoser().getNameLv();
        }

        worldMapOccupy.setMasterPlayerName(temp);

        worldMapOccupy.setOccupyPlayerName(occupyCityParam.getWinner().getNameLv());
        worldMapOccupy.setLoserPlayerName(occupyCityParam.getLoser()!=null?occupyCityParam.getLoser().getName():"");
        worldMapOccupy.setBeforeCamp(occupyCityParam.getBeforeCamp());
        worldMapOccupy.setAfterCamp(occupyCityParam.getWinner().getCamp());

        worldMapOccupyList.add(worldMapOccupy);
    }

    public void addChat(SGCommonProto.ChatItem chatItem)
    {
        chatList.add(chatItem);
    }

    public void addPlayerGoodsChange(PlayerGoodsChange playerGoodsChange)
    {
        goodsChangeList.add(playerGoodsChange);
    }

    public List<PlayerGoodsChange> getGoodsChangeList() {
        return goodsChangeList;
    }

    public void setGoodsChangeList(List<PlayerGoodsChange> goodsChangeList) {
        this.goodsChangeList = goodsChangeList;
    }

    public List<SGCommonProto.ChatItem> getChatList() {
        return chatList;
    }

    public void setChatList(List<SGCommonProto.ChatItem> chatList) {
        this.chatList = chatList;
    }
}
